<?php
namespace app\users\controller;
use app\common\controller\HomeCommon;
use think\Request;
use think\Validate;
use think\db\Query;
use think\Db;



/**
 * 1、发布动态
 * 2、点赞、取消点赞
 * @author admin
 *
 */
class Trends extends HomeCommon
{
	public function __construct(){
		parent::__construct();
		$token=input('token');
		//验证token
		$result=$this->check_token($token);
		
		if($result['errcode']!=0){
			
			echo json_encode(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);die;
		}
	}
	/**
	 * 1、发布动态
	 */
	public  function publish(){
		if(Request::instance()->isPost()){
			
			
			$validate = new Validate([
					'title'  => 'require|length:1,20',
					'desc' => 'require|length:1,500',
					'park_id' => 'require',
					
			]);
			$data = [
					'title'  => empty(input('title'))?'':input('title'),
				
					'desc' => empty(input('desc'))?'':input('desc'),
					'images' => empty(input('images'))?'':input('images'),
					
					'park_id' => empty(input('park_id'))?'':input('park_id'),
			];
			if (!$validate->check($data)) {
				return json(['errcode'=>'10001','msg'=>$validate->getError(),'data'=>[]]);
			}
			$data['user_id']=$this->user_id;
			if(db('trends')->insert($data)){
				return json(['errcode'=>'0','msg'=>'添加成功','data'=>['insertid'=>db('trends')->getLastInsID()]]);
			}else{
				return json(['errcode'=>'0','msg'=>'添加失败','data'=>[]]);
			}
			
			
		}
	}
	/**
	 *2、点赞、取消点赞
	 */
	public  function like(){
		if(Request::instance()->isPost()){
			$validate = new Validate([
					'trends_id'  => 'require|length:1,20',
					
			]);
			$data = [
					'trends_id'  => empty(input('trends_id'))?'':input('trends_id'),
					
					'park_id' => empty(input('park_id'))?'':input('park_id'),
			];
			if (!$validate->check($data)) {
				return json(['errcode'=>'10001','msg'=>$validate->getError(),'data'=>[]]);
			}
			
			//判断动态是否存在
			$info=db('trends')->field('id')->where(['id'=>$data['trends_id'],'status'=>1])->find();
			if(!$info){
				return json(['errcode'=>'10002','msg'=>'信息不存在或已删除','data'=>[]]);
			}
			
			$like_info=db('trends_like')->field('id,status')->where(['trends_id'=>$data['trends_id'],'user_id'=>$this->user_id])->find();
			if($like_info){
				if($like_info['status']==1){
					//取消点赞
					$res=db('trends_like')->update(['id'=>$like_info['id'],'status'=>0]);
					db('trends')->update(['id'=>$data['trends_id'],'like_num' => Db::raw('like_num-1')]);
					return json(['errcode'=>'10001','msg'=>'取消点赞成功','data'=>[]]);
				}else{
					//点赞
					$res=db('trends_like')->update(['id'=>$like_info['id'],'status'=>1]);
					db('trends')->update(['id'=>$data['trends_id'],'like_num' => Db::raw('like_num+1')]);
					return json(['errcode'=>'10001','msg'=>'点赞成功','data'=>[]]);
				}
			}else{
				$res=db('trends_like')->insert(['trends_id'=>$data['trends_id'],'status'=>1,'user_id'=>$this->user_id]);
				db('trends')->update(['id'=>$data['trends_id'],'like_num' => Db::raw('like_num+1')]);
				return json(['errcode'=>'10001','msg'=>'点赞成功','data'=>[]]);
			}
			
			
	        
			 
		}
	}
	
}